package com.travel.server.manager.impl.command.article;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.article.UpdateArticleClassRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ArticleClassDal;
import com.travel.server.manager.model.ArticleClassInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:17
 * @Desc:
 */
@Component
public class UpdateArticleClassCommand extends AbstractCommand<UpdateArticleClassRequest, EmptyResponse> {

    @Autowired
    private ArticleClassDal classDal;

    @Override
    protected void checkInput(UpdateArticleClassRequest request) {

    }

    @Override
    public EmptyResponse handle(UpdateArticleClassRequest request) {
        Date now = new Date();
        UserInfo userInfo = getUserInfo();

        ArticleClassInfo articleClassInfo;
        if(request.getId() == null){
            articleClassInfo = new ArticleClassInfo();
            articleClassInfo.setCreateBy(userInfo.getName());
            articleClassInfo.setCreateTime(now);
            articleClassInfo.setState(CommonConstant.YES);
        }else{
            articleClassInfo = classDal.selectById(request.getId());
        }
        articleClassInfo.setClassName(request.getName());
        articleClassInfo.setSort(request.getSort());
        articleClassInfo.setUpdateBy(userInfo.getName());
        articleClassInfo.setUpdateTime(now);
        classDal.insertOrUpdate(articleClassInfo);
        return new EmptyResponse();
    }
}
